home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / JToggleButton.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  298 lines

  1. /*
  2.  * @(#)JToggleButton.java    1.29 98/03/30
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not disclose
  8.  * such Confidential Information and shall use it only in accordance with the
  9.  * terms of the license agreement you entered into with Sun.
  10.  * 
  11.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  12.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  14.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  15.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  16.  * ITS DERIVATIVES.
  17.  * 
  18.  */
  19. package com.sun.java.swing;
  20.  
  21. import java.awt.*;
  22. import java.awt.event.*;
  23.  
  24. import com.sun.java.swing.event.*;
  25. import com.sun.java.swing.plaf.*;
  26. import com.sun.java.accessibility.*;
  27.  
  28. /**
  29.  * An implementation of a two-state button.  
  30.  * The <code>JRadioButton</code> and <code>JCheckBox</code> classes
  31.  * are subclasses of this class.
  32.  * <p>
  33.  * For the keyboard keys used by this component in the standard Look and
  34.  * Feel (L&F) renditions, see the
  35.  * <a href="doc-files/Key-Index.html#JToggleButton">JToggleButton</a> key assignments.
  36.  * <p>
  37.  * Warning: serialized objects of this class will not be compatible with
  38.  * future swing releases.  The current serialization support is appropriate
  39.  * for short term storage or RMI between Swing1.0 applications.  It will
  40.  * not be possible to load serialized Swing1.0 objects with future releases
  41.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  42.  * baseline for the serialized form of Swing objects.
  43.  *
  44.  * @beaninfo
  45.  *   attribute: isContainer false
  46.  * @see JRadioButton
  47.  * @see JCheckBox
  48.  * @version 1.29 03/30/98
  49.  * @author Jeff Dinkins
  50.  */
  51. public class JToggleButton extends AbstractButton implements Accessible {
  52.  
  53.     /**
  54.      * Creates an initially unselected toggle button
  55.      * without setting the text or image.
  56.      */
  57.     public JToggleButton () {
  58.         this(null, null, false);
  59.     }
  60.  
  61.     /**
  62.      * Creates an initially unselected toggle button
  63.      * with the specified image but no text.
  64.      *
  65.      * @param icon  the image that the button should display
  66.      */
  67.     public JToggleButton(Icon icon) {
  68.         this(null, icon, false);
  69.     }
  70.     
  71.     /**
  72.      * Creates a toggle button with the specified image 
  73.      * and selection state, but no text.
  74.      *
  75.      * @param icon  the image that the button should display
  76.      * @param selected  if true, the button is initially selected;
  77.      *                  otherwise, the button is initially unselected
  78.      */
  79.     public JToggleButton(Icon icon, boolean selected) {
  80.         this(null, icon, selected);
  81.     }
  82.     
  83.     /**
  84.      * Creates an unselected toggle button with the specified text.
  85.      *
  86.      * @param text  the string displayed on the toggle button
  87.      */
  88.     public JToggleButton (String text) {
  89.         this(text, null, false);
  90.     }
  91.  
  92.     /**
  93.      * Creates a toggle button with the specified text
  94.      * and selection state.
  95.      *
  96.      * @param text  the string displayed on the toggle button
  97.      * @param selected  if true, the button is initially selected;
  98.      *                  otherwise, the button is initially unselected
  99.      */
  100.     public JToggleButton (String text, boolean selected) {
  101.         this(text, null, selected);
  102.     }
  103.  
  104.     /**
  105.      * Creates a toggle button that has the specified text and image,
  106.      * and that is initially unselected.
  107.      *
  108.      * @param text the string displayed on the button
  109.      * @param icon  the image that the button should display
  110.      */
  111.     public JToggleButton(String text, Icon icon) {
  112.         this(text, icon, false);
  113.     }
  114.  
  115.     /**
  116.      * Creates a toggle button with the specified text, image, and
  117.      * selection state.
  118.      *
  119.      * @param text the text of the toggle button.
  120.      * @param selected  if true, the button is initially selected;
  121.      *                  otherwise, the button is initially unselected
  122.      */
  123.     public JToggleButton (String text, Icon icon, boolean selected) {
  124.         // Create the model
  125.         setModel(new ToggleButtonModel());
  126.  
  127.         model.setSelected(selected);
  128.  
  129.         // initialize
  130.         init(text, icon);
  131.     }
  132.  
  133.     /**
  134.      * Notification from the UIFactory that the L&F
  135.      * has changed. 
  136.      *
  137.      * @see JComponent#updateUI
  138.      */
  139.     public void updateUI() {
  140.         setUI((ButtonUI)UIManager.getUI(this));
  141.     }
  142.     
  143.     /**
  144.      * Returns a string that specifies the name of the l&f class
  145.      * that renders this component.
  146.      *
  147.      * @return String "ToggleButtonUI"
  148.      * @see JComponent#getUIClassID
  149.      * @see UIDefaults#getUI
  150.      * @beaninfo
  151.      *  description: A string that specifies the name of the L&F class
  152.      */
  153.     public String getUIClassID() {
  154.         return "ToggleButtonUI";
  155.     }
  156.  
  157.  
  158.  
  159.  
  160.     // *********************************************************************
  161.  
  162.     /**
  163.      * The ToggleButton model
  164.      * <p>
  165.      * Warning: serialized objects of this class will not be compatible with
  166.      * future swing releases.  The current serialization support is appropriate
  167.      * for short term storage or RMI between Swing1.0 applications.  It will
  168.      * not be possible to load serialized Swing1.0 objects with future releases
  169.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  170.      * baseline for the serialized form of Swing objects.
  171.      */
  172.     public static class ToggleButtonModel extends DefaultButtonModel {
  173.  
  174.         /**
  175.          * Creates a new ToggleButton Model
  176.          */
  177.         public ToggleButtonModel () {
  178.         }
  179.  
  180.         /**
  181.          * Checks if the button is selected.
  182.          */
  183.         public boolean isSelected() {
  184.             if(group != null) {
  185.                 return group.isSelected(this);
  186.             } else {
  187.                 return (stateMask & SELECTED) != 0;
  188.             }
  189.         }
  190.  
  191.  
  192.         /**
  193.          * Sets the selected state of the button.
  194.          * @param b true selects the toggle button,
  195.          *          false deselects the toggle button.
  196.          */
  197.         public void setSelected(boolean b) {
  198.             // if (this.isSelected() == b) {
  199.             // return;
  200.             // }
  201.                 
  202.             if(group != null) {
  203.                 // use the group model instead
  204.                 group.setSelected(this, b);
  205.             } else {
  206.                 if (b) {
  207.                     stateMask |= SELECTED;
  208.                 } else {
  209.                     stateMask &= ~SELECTED;
  210.                 }
  211.             }
  212.  
  213.             // Send ChangeEvent
  214.             fireStateChanged();
  215.  
  216.             // Send ItemEvent
  217.             fireItemStateChanged(
  218.                     new ItemEvent(this,
  219.                                   ItemEvent.ITEM_STATE_CHANGED,
  220.                                   this,
  221.                                   this.isSelected() ?  ItemEvent.SELECTED : ItemEvent.DESELECTED));
  222.         
  223.         }
  224.  
  225.         /**
  226.          * Sets the pressed state of the toggle button.
  227.          */ 
  228.         public void setPressed(boolean b) {
  229.             if ((isPressed() == b) || !isEnabled()) {
  230.                 return;
  231.             }
  232.  
  233.             if (b == false && isArmed()) {
  234.                 setSelected(!this.isSelected());
  235.             } 
  236.  
  237.             if (b) {
  238.                 stateMask |= PRESSED;
  239.             } else {
  240.                 stateMask &= ~PRESSED;
  241.             }
  242.  
  243.             fireStateChanged();
  244.  
  245.             if(!isPressed() && isArmed()) {
  246.                 fireActionPerformed(
  247.                     new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
  248.                                     getActionCommand())
  249.                     );
  250.             }
  251.  
  252.         }
  253.     }
  254.  
  255. /////////////////
  256. // Accessibility support
  257. ////////////////
  258.  
  259.     /**
  260.      * Get the AccessibleContext associated with this JComponent
  261.      *
  262.      * @return the AccessibleContext of this JComponent
  263.      * @beaninfo
  264.      *       expert: true
  265.      *  description: The AccessibleContext associated with this ToggleButton.
  266.      */
  267.     public AccessibleContext getAccessibleContext() {
  268.         if (accessibleContext == null) {
  269.             accessibleContext = new AccessibleJToggleButton();
  270.         }
  271.         return accessibleContext;
  272.     }
  273.  
  274.     /**
  275.      * The class used to obtain the accessible role for this object.
  276.      * <p>
  277.      * Warning: serialized objects of this class will not be compatible with
  278.      * future swing releases.  The current serialization support is appropriate
  279.      * for short term storage or RMI between Swing1.0 applications.  It will
  280.      * not be possible to load serialized Swing1.0 objects with future releases
  281.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  282.      * baseline for the serialized form of Swing objects.
  283.      */
  284.     protected class AccessibleJToggleButton extends AccessibleAbstractButton {
  285.  
  286.         /**
  287.          * Get the role of this object.
  288.          *
  289.          * @return an instance of AccessibleRole describing the role of the 
  290.          * object
  291.          */
  292.         public AccessibleRole getAccessibleRole() {
  293.             return AccessibleRole.TOGGLE_BUTTON;
  294.         }
  295.     } // inner class AccessibleJToggleButton
  296. }
  297.   
  298.